<template>
  <div class="forget">
    <div class="login-title">
      <p class="title">重置密码</p>
    </div>
    <el-form v-if="step == 1" ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="0">
      <el-form-item prop="phone" class="el-item_login">
        <el-input
          v-model.trim="ruleForm.phone"
          placeholder="请输入手机号"
          maxlength="11"
          type="tel"
          auto-complete="off"
          @input="ruleForm.phone = ruleForm.phone.replace(/[\u4E00-\u9FA5\s]/g, '')"
        />
      </el-form-item>
      <el-form-item prop="code" class="el-item">
        <el-input
          v-model.trim="ruleForm.code"
          placeholder="请输入验证码"
          maxlength="6"
          auto-complete="on"
          @input="ruleForm.code = ruleForm.code.replace(/[\u4E00-\u9FA5\s]/g, '')"
        >
          <template #suffix>
            <div v-if="count == 0" class="getcode" @click="getCode">获取验证码</div>
            <div v-else class="getcode">{{ count }}s重新获取</div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          class="login-btn"
          :loading="loading"
          :disabled="!(ruleForm.phone && ruleForm.code)"
          type="primary"
          color="#007cff"
          @click="step = 2"
          >下一步</el-button
        >
        <div class="goback" @click="goLogin">返回登录</div>
      </el-form-item>
    </el-form>
    <el-form v-if="step == 2" ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="0">
      <el-form-item prop="password" class="el-item">
        <el-input
          v-model.trim="ruleForm.password"
          placeholder="新密码"
          auto-complete="on"
          :type="passwordType"
          @input="ruleForm.password = ruleForm.password.replace(/[\u4E00-\u9FA5\s]/g, '')"
        >
          <template #suffix>
            <div class="show-pwd" @click="showPwd"
              ><svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            /></div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="newpassword" class="el-item">
        <el-input
          v-model.trim="ruleForm.newpassword"
          placeholder="确认密码"
          auto-complete="on"
          :type="passwordType2"
          @input="ruleForm.newpassword = ruleForm.newpassword.replace(/[\u4E00-\u9FA5\s]/g, '')"
        >
          <template #suffix>
            <div class="show-pwd" @click="showPwd2"
              ><svg-icon :icon-class="passwordType2 === 'password' ? 'eye' : 'eye-open'"
            /></div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          class="login-btn"
          :loading="loading"
          :disabled="!(ruleForm.password && ruleForm.newpassword)"
          type="primary"
          color="#007cff"
          @click="submitForm(ruleFormRef)"
          >提交</el-button
        >
        <div class="goback" @click="step = 1">返回上一步</div>
      </el-form-item>
    </el-form>
    <div v-if="step == 3" class="success">
      <el-icon color="#fd7400" size="90">
        <SuccessFilled />
      </el-icon>
      <div class="success_text"> 恭喜 密码已重置 </div>
      <div class="tip"> 请用新的密码进行登录系统 </div>
      <el-button class="login-btn" type="primary" color="#007cff" @click="goLogin"
        >返回登录</el-button
      >
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { ref, reactive, getCurrentInstance } from 'vue'

  import { ElMessage, FormInstance } from 'element-plus'
  import { updateCode, notLoginUpdatePassword } from '../../../api/user'
  import { password, phone, passwordLength } from '../../../utils/validate'
  import { useRouter } from 'vue-router'
  import { md5 } from 'js-md5'
  const ruleFormRef = ref<FormInstance>()
  const clear = ref(null)
  const router = useRouter()
  const passwordType = ref('password')
  const passwordType2 = ref('password')
  const loading = ref(false)
  const step = ref(1)
  const emit = defineEmits(['change-show-login'])
  // 登录页面
  const goLogin = () => {
    router.push({ path: '/login/index' })
  }

  //md5密码加密
  const { proxy } = getCurrentInstance() as any
  proxy.$md5 = md5 //md5转换为vue原型
  // 表单数据

  const ruleForm = reactive({
    phone: '',
    password: '',
    newpassword: '',
    code: '',
  })
  const rules = reactive({
    phone: [
      { required: true, message: '请输入手机号', trigger: 'blur' },
      { validator: phone(ruleForm.phone), trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      //  { validator: password(ruleForm.password), trigger: "blur" }//saas
      { validator: passwordLength(ruleForm.password), trigger: 'blur' }, //格美淇
    ],
    code: [
      { required: true, message: '请输入正确的验证码', trigger: 'blur' },
      { pattern: /^\d*$/, message: '请输入正确的验证码', trigger: 'blur' },
    ],
    newpassword: [
      { required: true, message: '请确认密码', trigger: 'blur' },
      {
        validator: (rule: any, value: string, callback: any) => {
          if (ruleForm.password != ruleForm.newpassword) {
            callback(new Error('密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur',
      },
    ],
  })

  // 倒计时
  const count = ref(0)
  const showPwd = () => {
    if (passwordType.value === 'password') {
      passwordType.value = ''
    } else {
      passwordType.value = 'password'
    }
  }
  const showPwd2 = () => {
    if (passwordType2.value === 'password') {
      passwordType2.value = ''
    } else {
      passwordType2.value = 'password'
    }
  }
  // 提交
  const submitForm = (formEl) => {
    if (!formEl) return
    loading.value = true
    formEl.validate((valid) => {
      if (valid) {
        let data = {
          phone: ruleForm.phone,
          newPassword: proxy.$md5(ruleForm.password), //md5加密
          oldPassword: ruleForm.code,
          type: 1,
        }
        notLoginUpdatePassword(data).then(async (res: any) => {
          loading.value = false
          if (res.data.success) {
            ElMessage({
              showClose: true,
              message: res.data.message,
              type: 'success',
              duration: 5000,
              plain: true,
            })
            step.value = 3
          } else {
            ElMessage({
              showClose: true,
              message: res.data.message,
              type: 'error',
              duration: 5000,
              plain: true,
            })
          }
        })
      } else {
        return false
      }
    })
  }

  // 获取验证码
  const getCode = () => {
    ruleFormRef.value.validateField('phone', (v) => {
      if (v) {
        count.value = 59
        clear.value = setInterval(() => {
          count.value -= 1
          if (count.value == 0) {
            clearInterval(clear.value)
          }
        }, 1000)
        updateCode({ phone: ruleForm.phone }).then((res) => {
          if (res.data.success) {
            ElMessage({
              showClose: true,
              message: res.data.message,
              type: 'success',
              duration: 5000,
              plain: true,
            })
          } else {
            ElMessage({
              showClose: true,
              message: res.data.message,
              type: 'error',
              duration: 5000,
              plain: true,
            })
          }
        })
      }
    })
  }
</script>

<style lang="scss" scoped>
  $dark_gray: #889aa4;

  .login-btn {
    width: 100%;

    height: 40px;
    font-weight: bold;
    margin-bottom: 10px;
    border-radius: 0;
  }

  :deep(.el-button > span) {
    font-size: 16px;
  }

  :deep(.el-form-item.is-error .el-input__wrapper) {
    box-shadow: 0 0 0 0 transparent !important;
    border-bottom: 1px solid #f56c6c !important;
  }

  .login-container {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;

    .main_box {
      height: 100%;

      .login-left_img {
        height: 100%;

        // margin-right: 480px;
        .login_img {
          min-width: 260px;
          height: 100%;
          /* 使用过渡效果 */
          background-size: cover;
          background-repeat: no-repeat;
          background-position: contain;
          animation: bg-image-animation-saas 30s infinite; //saas
          // animation: bg-image-animation 30s infinite ;//格美棋
          // animation: bg-image-animation-sld 40s infinite; //施罗德

          // filter: brightness(60%);
          .text_tip {
            position: relative;
            top: 20%;
            color: #fff;

            h3 {
              font-size: 32px;
              margin-bottom: 20px;
            }

            p {
              font-size: 16px;
            }
          }
        }

        // /* 定义动画 saas*/
        @keyframes bg-image-animation-saas {
          0%,
          33% {
            background-image: url('@/assets/login/login_bg1.jpg');
          }

          33%,
          66% {
            background-image: url('@/assets/login/login_bg2.jpg');
          }

          66%,
          100% {
            background-image: url('@/assets/login/login_bg3.jpg');
          }

          100% {
            background-image: url('@/assets/login/login_bg1.jpg');
          }
        }

        /* 定义动画  格美棋*/
        @keyframes bg-image-animation {
          0%,
          33% {
            background-image: url('@/assets/login/gemake/gemake_1.jpg');
          }

          33%,
          66% {
            background-image: url('@/assets/login/gemake/gemake_2.jpg');
          }

          66%,
          100% {
            background-image: url('@/assets/login/gemake/gemake_3.jpg');
          }

          100% {
            background-image: url('@/assets/login/gemake/gemake_1.jpg');
          }
        }

        /* 定义动画  施罗德*/
        @keyframes bg-image-animation-sld {
          0%,
          32% {
            background-image: url('@/assets/login/sld/1.jpg');
          }

          33%,
          65% {
            background-image: url('@/assets/login/sld/2.jpg');
          }

          66%,
          99% {
            background-image: url('@/assets/login/sld/3.jpg');
          }

          100% {
            background-image: url('@/assets/login/sld/1.jpg');
          }
        }
      }

      .login-box {
        position: relative;
        width: 480px;
        height: 100%;
        float: right;
        background-color: #ffffffe6;
        z-index: 2;
        overflow-y: auto;
        height: 100%;
        display: flex;
        flex-direction: column;

        .logo-big {
          text-align: center;
          vertical-align: middle;
          margin-top: 48px;
          display: flex;
          flex-direction: column;
          gap: 8px;
          align-items: center;

          .logo_image {
            width: 336px;
            height: 158px;
          }

          .name-logo {
            font-size: 32px;
            // color: #007cff;
            color: #000000a0;
            // font-weight: bold;
            width: 342px;
            letter-spacing: 6px;
            // text-shadow: 1px 1px 3px #007DFE;
            // text-shadow: 1px 1px 3px #000000bd;
          }
        }

        .login-form {
          padding: 40px 40px 0px;
          box-sizing: border-box;
          flex: 1;

          .text {
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;

            .goRegister {
              color: #007cff;
              font-size: 14px;
              display: flex;
              align-items: center;
              cursor: pointer;

              &:hover {
                text-decoration: underline;
              }
            }
          }
        }

        .login-title {
          display: flex;
          flex-direction: column;
          margin-bottom: 10px;

          .title {
            margin: 0;
            font-size: 14px;
            text-align: left;
            letter-spacing: 2px;
          }
        }
      }
    }

    .footer_text {
      font-size: 12px;
      padding-bottom: 10px;
      position: relative;
      bottom: 0;
      text-align: center;
      width: 100%;
      box-sizing: border-box;
      z-index: 2;

      .url_link {
        color: #999;
      }
    }
  }

  .login-dark {
    position: absolute;
    right: 20px;
    top: 20px;
  }

  .tabs {
    height: 40px;
    width: 100%;
    background-color: #f3f7fb;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    border-radius: 0;
    justify-content: space-between;

    .tab_display {
      display: flex;
    }

    .tab {
      padding: 2px;
      color: #999;
      margin: 0 8px;
      font-size: 14px;
      border-bottom: 2px solid transparent;
      cursor: pointer;
    }

    .tab_temp {
      color: #007cff;
      border-bottom: 2px solid #007cff;
    }
  }

  @media screen and (max-width: 480px) {
    .login-container {
      .main_box {
        .login-box {
          width: 100%;

          .login-form {
            padding: 10% 24px 20px;
          }
        }
      }
    }
  }

  :deep(.el-input-number) {
    width: 100%;

    .el-input__inner {
      text-align: left;
    }

    .el-input__wrapper {
      padding: 1px 11px;
    }
  }

  .el-item {
    margin-bottom: 20px;
  }

  .el-item_login {
    margin-bottom: 30px;
  }

  :deep(.el-input__wrapper) {
    background-color: transparent;
    border-radius: 0;
    height: 35px;
    box-shadow: 0 0 0 1px transparent;
    border-bottom: 1px solid #dcdfe6 !important;

    &.is-focus {
      box-shadow: 0 0 0 1px transparent;
      border-bottom: 1px solid #007cff !important;
    }
  }

  :deep(.el-input .el-input__wrapper) {
    box-sizing: border-box;

    &.is-focus {
      box-shadow: 0 0 0 1px transparent;
      border-bottom: 1px solid #007cff !important;
    }
  }

  .check {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    margin: 10px 0;

    .check_left {
      span {
        color: #000;
      }

      .agree_re {
        display: flex;
        color: #999;

        span {
          color: #007cff;
          cursor: pointer;
        }
      }
    }

    .check_right {
      cursor: pointer;
    }
  }

  .agree {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    color: #999;
    margin-top: 12px;
    margin-bottom: 6px;
    font-size: 12px;
    white-space: pre-wrap;

    a {
      color: #007cff;
      cursor: pointer;
    }
  }

  .getcode {
    color: #007cff;
    cursor: pointer;
  }

  :deep(.el-form-item__error) {
    padding-top: 4px;
  }

  .forget {
    .login-btn {
      font-size: 16px;
      border: none !important;
      margin: 20px auto 0;
    }

    .goback {
      color: #007cff;
      font-size: 14px;
      text-align: center;
      margin: 10px auto 0;
      cursor: pointer;

      &:hover {
        text-decoration: underline;
      }
    }

    .success {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      width: 100%;
      margin-top: 40px;

      .success_text {
        color: #fd7400;
        font-size: 14px;
        margin: 20px;
      }

      .tip {
        color: #999;
        font-size: 14px;
      }
    }
  }
</style>
